package core

// Environment 表示Scheme环境，用于存储变量和过程绑定
type BuiltinFunction func(args []Object, env *Environment) Object

// Builtin 表示内置过程对象
type Builtin struct {
	Fn BuiltinFunction
}

// Type 返回对象类型
func (b *Builtin) Type() ObjectType {
	return BUILTIN_OBJ
}

// String 返回内置过程的字符串表示
func (b *Builtin) String() string {
	return "builtin function"
}

// Inspect 返回内置过程的详细表示
func (b *Builtin) Inspect() string {
	return "builtin function"
}

type Environment struct {
	store map[string]Object
	outer *Environment
}

func NewEnvironment() *Environment {
	return &Environment{store: make(map[string]Object), outer: nil}
}

func NewEnclosedEnvironment(outer *Environment) *Environment {
	env := NewEnvironment()
	env.outer = outer
	return env
}

func (e *Environment) Get(name string) (Object, bool) {
	obj, ok := e.store[name]
	if !ok && e.outer != nil {
		obj, ok = e.outer.Get(name)
	}
	return obj, ok
}

func (e *Environment) Set(name string, val Object) Object {
	e.store[name] = val
	return val
}
